$(function () {
    /*
     * 抽取所有需要用得元素.
     */
    var clientResourcePoolDatagrid =  $("#clientResourcePool_datagrid");
    /*
     * 初始化数据表格
     */
    clientResourcePoolDatagrid.datagrid({
        url: "/clientResourcePool/list",
        fit: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        fitColumns: true,
        toolbar: '#clientResourcePool_datagrid_tb',
        columns: [
            [
                {field: 'clientsn', width: 10, align: 'center', title: '客户编号'},
                {field: 'username', width: 10, align: 'center', title: '用户名'},
                {field: 'clientResourcePooltype', width: 10, align: 'center', title: '客户类型',formatter: function (value,row,index) {
                   return value==1?"公司法人":"个人";
                }},
                {field: 'status', width: 10, align: 'center', title: '状态',formatter: function (value,row,index) {
                    if(value==1){
                        return "正式客户";
                    }else if(value==0){
                        return "潜在客户";
                    }else{
                        return "无效客户";
                    }
                }},
                {field: 'realname', width: 10, align: 'center', title: '真实姓名'},
                {field: 'idcardnumber', width: 10, align: 'center', title: '身份证号码'},
                {field: 'gender', width: 10, align: 'center', title: '性别',formatter: function (value,row,index) {
                    return value==1?"男":"女";
                }},
                {field: 'phonenumber', width: 10, align: 'center', title: '电话号码'},
                {field: 'email', width: 10, align: 'center', title: '邮箱'},
                {field: 'address', width: 10, align: 'center', title: '地址'},
                {field: 'applicant', width: 10, align: 'center', title: '是否投保人',formatter: function (value,row,index) {
                    if(value==1){
                        return "车主";
                    }else if(value==0){
                        return "投保人";
                    }else{
                        return "被投保人";
                    }
                }},
                {field: 'inputtime', width: 10, align: 'center', title: '录入时间'},
                {field: 'remark', width: 10, align: 'center', title: '备注'},
                {field: 'inputor', width: 10, align: 'center', title: '录入人',formatter: function (value,row,index) {
                   return value ? value.realname :"无";
                }},
                {field: 'manager', width: 10, align: 'center', title: '负责人',formatter: function (value,row,index) {
                    return value ? value.realname :"无";
                }},
                {field: 'sharer', width: 10, align: 'center', title: '共享人',formatter: function (value,row,index) {
                    return value ? value.realname :"无";
                }}
            ]
        ]
    });

    $("#searchBtn").textbox({
        width: 230,
        label: "关键字:",
        labelWidth: 50,
        prompt: "请输入搜索关键字",
        buttonText: '搜索',
        buttonIcon: 'icon-search',
        onClickButton: function () {
            var keyword = $(this).val();
            clientResourcePoolDatagrid.datagrid("load", {
                keyword: keyword
            });
        }
    });
    /*
     * 对页面按钮进行统一监听
     */
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        if (cmd) {
            cmdObj[cmd]();
        }
    });
    /*
     * 所有的操作封装到cmdObj对象中,方便管理
     */
    var cmdObj = {
        acquire: function () {
            var rowData = clientResourcePoolDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要领取此客户吗？", function (yes) {
                    if (yes) {
                        $.get("/clientResourcePool/acquire?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    clientResourcePoolDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要领取的客户数据!", "warining");
            }
        },
        reload: function () {
            clientResourcePoolDatagrid.datagrid("reload");
        }
    }
});
